


//The following code is taken from sleep.h as Arduino Software v22 (avrgcc), in w32 does not have the latest sleep.h file
#define sleep_bod_disable() \
{ \
  uint8_t tempreg; \
  __asm__ __volatile__("in %[tempreg], %[mcucr]" "\n\t" \
                       "ori %[tempreg], %[bods_bodse]" "\n\t" \
                       "out %[mcucr], %[tempreg]" "\n\t" \
                       "andi %[tempreg], %[not_bodse]" "\n\t" \
                       "out %[mcucr], %[tempreg]" \
                       : [tempreg] "=&d" (tempreg) \
                       : [mcucr] "I" _SFR_IO_ADDR(MCUCR), \
                         [bods_bodse] "i" (_BV(BODS) | _BV(BODSE)), \
                         [not_bodse] "i" (~_BV(BODSE))); \
}

DS3231 RTC; //Create RTC object for DS3231 RTC come Temp Sensor 
static uint8_t prevSecond = 0; 
static DateTime interruptTime;

// store error strings in flash to save RAM
#define error(s) error_P(PSTR(s))


#define DIGITAL_PIN2_INTERRUPT 0
#define DIGITAL_PIN3_INTERRUPT 1

void attachMyInterrupt()
{
  attachInterrupt(DIGITAL_PIN2_INTERRUPT, interruptTreatmentFunct, LOW); //Only LOW level interrupt can wake up from PWR_DOWN
}

void setupSleepAndPowerOn()
{
     /*Initialize INT0 pin for accepting interrupts */
     PORTD |= 0x04; 
     DDRD &=~ 0x04;

     //pinMode(4,INPUT);//extern power //JMCM CREO QUE ESTO ES PARA ALIMENTAR LA TARJETA SD. YO NO LO USO

     Wire.begin(); //para la temperatura del RTC 
     RTC.begin();
     
     attachMyInterrupt();
     set_sleep_mode(SLEEP_MODE_PWR_DOWN);
 
     //Enable Interrupt 
     RTC.enableInterrupts(EverySecond); //interrupt at  EverySecond, EveryMinute, EveryHour
}


void manageSleepCycle()
{
     
    RTC.clearINTStatus(); //This function call is  a must to bring /INT pin HIGH after an interrupt.
    attachMyInterrupt();
    
    //\/\/\/\/\/\/\/\/\/\/\/\/Sleep Mode and Power Down routines\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
    Serial.println("Powering down");            
    //Power Down routines
    cli(); 
    sleep_enable();      // Set sleep enable bit
    sleep_bod_disable(); // Disable brown out detection during sleep. Saves more power
    sei();
        
    Serial.println("\nSleeping");
    delay(10); //This delay is required to allow print to complete
    //Shut down all peripherals like ADC before sleep. Refer Atmega328 manual
    power_all_disable(); //This shuts down ADC, TWI, SPI, Timers and USART
    sleep_cpu();         // Sleep the CPU as per the mode set earlier(power down)  
    
    //######### Aquí nos quedamos hasta que entre la interrupción... #############
    
    
    sleep_disable();     // Wakes up sleep and clears enable bit. Before this ISR would have executed
    power_all_enable();  //This shuts enables ADC, TWI, SPI, Timers and USART
    delay(10); //This delay is required to allow CPU to stabilize
    Serial.println("Awake from sleep");    
    
    //\/\/\/\/\/\/\/\/\/\/\/\/Sleep Mode and Power Saver routines\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\

}


//Interrupt service routine for external interrupt on INT0 pin conntected to DS3231 /INT
void interruptTreatmentFunct()
{
  //Keep this as short as possible. Possibly avoid using function calls
    detachInterrupt(0); 
}
